/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import java.sql.*;
import java.util.*;

import blueprint4j.utils.*;

public class VectorEntity implements java.io.Serializable {
    private Vector store = new Vector();
    
    public VectorEntity(){
    }
    
    public VectorEntity(Entity entity[]){
        for (int i =0;i < entity.length;i++) {
            add(entity[i]);
        }
    }
    
    public VectorEntity(Entity entity)
    throws DataException,SQLException {
        this(entity,-1);
    }
    
    public VectorEntity(Entity entity,int limit)
    throws DataException,SQLException {
        for (int t=0;(t<limit || limit==-1) && entity!=null;entity=entity.getNextEntity()){
            add(entity);
        }
    }
    
    public void removeNull() {
        for (int i =0;i < size();i++) {
            if (get(i) == null) {
                remove(i--);
            }
        }
    }
    
    public Entity get(int pos) {
        return (Entity)store.get(pos);
    }
    
    /**
     * Retrieves an entity refrenced by its id
     */
    public Entity get(String id)
    throws DataException {
        for (int i =0;id != null && i < size();i++) {
            if (id.equals(get(i).id.getAsString())) {
                return get(i);
            }
        }
        return null;
    }
    
    public Entity get(Class entity_class,Field key_field)
    throws DataException {
        for (int i =0;entity_class != null && key_field != null && i < size();i++) {
            for (int e = 0;get(i).getClass().getName().equals(entity_class.getName()) && e < get(i).getFields().size();e++) {
                if (
                        get(i).getFields().get(e).getName().equals(key_field.getName()) &&
                        get(i).getFields().get(e).getObject().equals(key_field.getObject())
                        ) {
                    return get(i);
                }
            }
        }
        return null;
    }
    
    /**
     * Returns an array of all the name fields
     */
    public List<String> getName() {
        List<String> vs = new ArrayList<String>();
        for (int i = 0;i < size();i++) {
            vs.add(get(i).getNameFields().toString(","));
        }
        return vs;
    }
    
    /**
     * Returns an array of all the id fields
     */
    public List<String> getId()
    throws DataException {
        List<String> vs = new ArrayList<String>();
        for (int i = 0;i < size();i++) {
            vs.add(get(i).id.getAsString());
        }
        return vs;
    }
    
    public boolean contains(Entity item){
        return store.contains(item);
    }
    
    public void add(int index,Entity item) {
        store.add(index,item);
    }
    
    public void add(Entity item) {
        store.add(item);
    }
    
    public void addRecursive(Entity item) throws SQLException {
        for (;item != null;item = item.getNextEntity()) {
            add(item);
        }
    }
    
    public void add(VectorEntity items) {
        for (int i =0;items != null && i < items.size();i++) {
            add(items.get(i));
        }
    }
    
    public void set(int index,Entity entity) {
        store.set(index,entity);
    }
    
    public boolean remove(Entity item) {
        return store.remove(item);
    }
    
    public Entity remove(int pos) {
        return (Entity) store.remove(pos);
    }
    
    public int size() {
        return store.size();
    }
    
    public int indexOf(Entity entity) throws DataException {
        if (store.indexOf(entity) > -1) {
            return store.indexOf(entity);
        }
        for (int i = 0;i < size();i++) {
            if (get(i).getTableName().equals(entity.getTableName()) && entity.id.getAsString().equals(get(i).id.getAsString())) {
                return i;
            }
        }
        return -1;
    }
    
    public void save()
    //throws DataException , SQLException {
    throws BindException {
        for (int t = 0;t < size();t++) {
            get(t).save();
        }
    }
    
    public void removeAll(){
        store.removeAllElements();
    }
    
    public void deleteAll()
    //throws DataException , SQLException {
    throws BindException {
        for (int t = 0;t < size();t++) {
            get(t).delete();
        }
    }
    
    public void reload()
    //throws DataException , SQLException {
    throws BindException {
        for (int t = 0;t < size();t++) {
            if (get(t).isLoaded()){
                get(t).reload();
            }
        }
    }
    
    public Object[][] getNameFieldValue() throws DataException {
        if (size() == 0) {
            return new Object[][]{{}};
        }
        Object data[][]= new Object[size()][get(0).getFields().getNameField().size()];
        for (int i = 0;i < size();i++) {
            data[i] = get(i).getFields().getNameField().getValue();
        }
        return data;
    }
    
    public VectorProperties toProperties() throws BindException {
        VectorProperties vp = new VectorProperties();
        for (int i = 0;i < size();i++) {
            vp.add(get(i).toProperties());
        }
        return vp;
    }
    
    public static VectorEntity toVectorEntity(DBConnection dbcon,VectorProperties vp) throws BindException {
        VectorEntity ve = new VectorEntity();
        for (int i = 0;i < vp.size();i++) {
            //System.out.println("toVectorEntity " + vp.get(i));
            ve.add(Entity.fromProperties(dbcon,vp.get(i)));
            //System.out.println("Entity now =  " + ve.get(ve.size()-1).getFieldId().getAsString());
        }
        return ve;
    }
}

